# from rest_framework import serializers
#
# from apps.recruit.models import Enterprise, Recruit, City
#
#
# class EnterpriseSerializerSimple(serializers.ModelSerializer):
#     recruits = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
#
#     class Meta:
#         model = Enterprise
#         fields = ('id', 'name', 'labels', 'logo', 'recruits', 'summary')
#
#
# class RecruitSerializerSimple(serializers.ModelSerializer):
#     enterprise = EnterpriseSerializerSimple(read_only=True)
#
#     class Meta:
#         model = Recruit
#         fields = ('id', 'jobname', 'salary', 'condition', 'education', 'type', 'city', 'createtime', 'enterprise', 'labels')
#
#
# class CitySerializer(serializers.ModelSerializer):
#     class Meta:
#         model = City
#         fields = "__all__"
#
#
# class EnterpriseSerializer(serializers.ModelSerializer):
#     recruits = RecruitSerializerSimple(many=True, read_only=True)
#
#     class Meta:
#         model = Enterprise
#         fields = "__all__"
#
#
# class RecruitSerializer(serializers.ModelSerializer):
#     enterprise = EnterpriseSerializer(read_only=True)
#
#     class Meta:
#         model = Recruit
#         fields = "__all__"

from django.db import models
from rest_framework.response import Response

from rest_framework import serializers

from apps.recruit.models import Enterprise, Recruit, City


class EnterpriseSerializer(serializers.ModelSerializer):
    # 企业序列化器
    recruits = serializers.PrimaryKeyRelatedField(label='职位ID', read_only=True,many=True)

    class Meta:
        model = Enterprise
        fields = ('id', 'name', 'labels', 'logo', 'recruits', 'summary')


class RecruitSerializer(serializers.ModelSerializer):
    # 职位序列化器
    enterprise = EnterpriseSerializer(label="企业ID")

    class Meta:
        model = Recruit
        fields = (
        'id', 'jobname', 'salary', 'condition', 'education', 'type', 'city', 'createtime', 'enterprise', 'labels')

class CitySerializer(serializers.ModelSerializer):
    # 城市序列化器
    class Meta:
        model = City
        fields = '__all__'


class EnterpriseInfoSerializer(serializers.ModelSerializer):
    # 公司详情序列化器
    recruits = RecruitSerializer(label='职位详情',many=True)

    class Meta:
        model = Enterprise
        fields = '__all__'



class RecruitInfoSerializer(serializers.ModelSerializer):
    # 职位详情序列化器
    enterprise = EnterpriseInfoSerializer(label='企业序列化器类')
    class Meta:
        model = Recruit
        fields = '__all__'

    def update(self, instance, validated_data):
        job_id = instance.id
        job = Recruit.objects.get(id=job_id)
        job.visits += 1
        job.save()
        return Response({"message":"更新成功","success":True})
